using UnityEngine;
using System.Collections;
using System.Threading;

public class Threading : MonoBehaviour {
	Threading Log;
	Threading Log1;
	
	public float speed = 10; // Speed of Object.
	public Vector3 target1p; // For position.
	public Vector3 target1s; // For scale.
	public Quaternion target1r; //For rotation.
	public bool run = true; // Starts the thread at start.
	public int timing = 5000; //Hidden log timer.
	public int writeOut = 5000; //Hidden write timer.
	int thatTimer; // Used log timer.
	int writeTimer; // Used write timer.
	Thread t;
	Thread s;
	
	static System.Collections.Generic.Queue<float> trackedNumbers; // For tracking information.
	
	// Use this for initialization
	void Start () {
	
		Log = new Threading();
		Log1 = new Threading();
		Log.thatTimer = timing;
		
		t = new System.Threading.Thread(new System.Threading.ThreadStart(Log.Run)); //Makes Thread.
		
		Log1.writeTimer = writeOut;
		t.Start(); //Starts thread.
		
		s = new System.Threading.Thread(new System.Threading.ThreadStart(Log1.writing)); //Makes Thread.
		s.Start(); //Starts thread.

	}
	
	// Update is called once per frame
	void Update () {
	
		transform.position = transform.position + (speed * Time.deltaTime) * new Vector3(1, 0, 0);
		
		target1p = transform.position; //for OnCollisionEnter.
		target1s = transform.lossyScale; //for OnCollisionEnter.
		target1r = transform.rotation; // for OnCollisionEnter.
		
		Log.target1p = transform.position; //for Run x,y,z.
		Log.target1s = transform.lossyScale; //for Run scale x,y,z.
		Log.target1r = transform.rotation; // for Run rotation x,y,z.
		
		
		if(Input.GetKeyDown(KeyCode.Return))
		{
			Log.run = false;
			//Stops thread on button press.
		}
	}
	public Threading()
	{
		trackedNumbers = new System.Collections.Generic.Queue<float>();
		//Collection system used to store tracked information.
	}

	public void Run()
	{
		while(run)
		{
			//Logs all the tracked info.
			Thread.Sleep(thatTimer);
			trackedNumbers.Enqueue(speed); // Logs speed.
			trackedNumbers.Enqueue(target1p.x); // Logs x Position.
			trackedNumbers.Enqueue(target1p.y); // Logs y Position.
			trackedNumbers.Enqueue(target1p.z); // Logs z Position.
			trackedNumbers.Enqueue(target1s.x); // Logs x Scale.
			trackedNumbers.Enqueue(target1s.y); // Logs y Scale.
			trackedNumbers.Enqueue(target1s.z); // Logs z Scale.
			trackedNumbers.Enqueue(target1r.x); // Logs x Rotation.
			trackedNumbers.Enqueue(target1r.y); // Logs y Rotation.
			trackedNumbers.Enqueue(target1r.z); // Logs z Rotation.
			UnityEngine.Debug.Log("written value");

		}
		
		
	}
	
	public void writing()
	{
		
		while(run)
		{
		Thread.Sleep (writeTimer);
		//Writes all the tracked info.
			System.IO.StreamWriter write = new System.IO.StreamWriter("Log.txt");
		
			while(trackedNumbers.Count > 0)
			{
				write.WriteLine(trackedNumbers.Dequeue().ToString());
				//Clears the information in the Collection.
			}
			Debug.Log("writing dawg");
			write.Close();
		}	
			
	}
		
	public void OnCollisionEnter(Collision collision)
	{
		//Logs Collision info.
		trackedNumbers.Enqueue (timing); // Logs timing.
		trackedNumbers.Enqueue(target1p.x); // Logs x Position.
		trackedNumbers.Enqueue(target1p.y); // Logs y Position.
		trackedNumbers.Enqueue(target1p.z); // Logs z Position.
		trackedNumbers.Enqueue(target1s.x); // Logs x Scale.
		trackedNumbers.Enqueue(target1s.y); // Logs y Scale.
		trackedNumbers.Enqueue(target1s.z); // Logs z Scale.
		trackedNumbers.Enqueue(target1r.x); // Logs x Rotation.
		trackedNumbers.Enqueue(target1r.y); // Logs y Rotation.
		trackedNumbers.Enqueue(target1r.z);	// Logs z Rotation.
		UnityEngine.Debug.Log("We hit something");
	}
	
	
	
	void OnApplicationQuit()
	{
		if(t.IsAlive)
		{
			Log.run = false;
			//Lets the thread finish.
			t.Join();
			
			
		}
		if(s.IsAlive)
		{
			Log1.run = false;
			//Kills the thread.
			s.Abort();

		}
		
	}
}
